package com.kevin.action.back;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import lombok.Data;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessionFilter;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.View;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.filter.CheckSession;
import org.nutz.mvc.view.JspView;
import org.nutz.mvc.view.ServerRedirectView;
import org.nutz.mvc.view.ViewWrapper;

import com.kevin.action.BaseAction;
import com.kevin.chat.User;
import com.kevin.chat.UserManager;
import com.kevin.dao.ManagerDao;
import com.kevin.model.Manager;
import com.kevin.model.Role;


/**
 * 首页
 *
 * @author gevin(gevinhjy@foxmail.com)
 *
 */
@IocBean
@At("/admin")
public  class LoginAction extends BaseAction{
	@Inject
	private ManagerDao managerDao;
	
	/**
	 * 查询首页数据
	 * 
	 * @param ioc
	 * @param req
	 * @return
	 */
	@At("/")
	@Ok("redirect:/admin/loginp.shtml")
	@Fail("jsp:error.404")
	public void indexpage() {
	}
	/**
	 * 
	  * @方法名  loginp 
	  * @功能描述 登陆页
	  * @编码人   吕凯
	  * @编码时间  2012-10-19 下午01:04:13 
	  * @输入参数    
	  * @返回值  void
	 */
	@At
	@Ok("jsp:/jsp/back/login")
	@Fail("jsp:error.404")
	public void loginp(HttpServletRequest req) {
	}
	@At
	@Ok("jsp:/jsp/back/index")
	@Fail("jsp:error.404")
	@Filters( { @By(type = CheckSession.class, args = { "admin", "/admin/loginp.shtml" }) })
	public void index() {
	}
	/**
	 * 
	  * @方法名  login 
	  * @功能描述  后台管理员登陆
	  * @编码人   吕凯
	  * @编码时间  2012-10-19 上午10:50:24 
	  * @输入参数   @return 
	  * @返回值  Map<String,Object>
	 */
	@At
	@Fail("jsp:error.404")
	public View login(	
			@Param("username") String username,
			@Param("password") String password,
			HttpServletRequest req) {
		Manager m = managerDao.login(username, password);
		if (m != null && m.isState()) {

			m = managerDao.findLink(m, "roles");

			List<Role> roles = m.getRoles();
			List<Role> temp = new ArrayList<Role>();
			for (Role r : roles) {
				r = managerDao.findLink(r, "permission");
				temp.add(r);
			}
			m.setRoles(temp);
			roles = null;// 释放资源

			req.getSession().setAttribute("admin", m);

			if (m.getLoginTime() != null) {
				m.setLastLoginTime(m.getLoginTime());
			}
			if (m.getLoginIp() != null) {
				m.setLastLoginIp(m.getLoginIp());
			}
			m.setLoginTime(new Date());
			m.setLoginIp(req.getRemoteAddr());
			m.setLogintimes(m.getLogintimes() + 1);

			managerDao.update(m);

			return new ViewWrapper(new ServerRedirectView("/admin/index.shtml"), m);
		} else {
			if (m == null) {
				req.setAttribute("error", "用户名或者密码错误！");
			} else {
				req.setAttribute("error", "用户已经被禁用！");
			}
			return new JspView("/jsp/back/login");
		}
	}
	/**
	 * 
	  * @方法名  logout 
	  * @功能描述  后台管理员登出
	  * @编码人   吕凯
	  * @编码时间  2012-10-19 上午10:50:38 
	  * @输入参数   @return 
	  * @返回值  Map<String,Object>
	 */
	@At
	@Ok("redirect:/admin/index.shtml")
	@Fail("jsp:error.404")
	public void logout(final HttpServletRequest req) {
		//删除scriptsession
		Browser.withAllSessionsFiltered(new ScriptSessionFilter() {
			
			public boolean match(ScriptSession arg0) {
				if(arg0.getAttribute("userid").equals(req.getSession().getId())){
					arg0.invalidate();
					return true;
				}
				return false;
			}
		}, new SendMessageThread());
		//删除用户
		UserManager.removeUser(new User(req.getSession().getId(), ""));
		req.getSession().removeAttribute("admin");
	}
	@Data class SendMessageThread implements Runnable{
		public SendMessageThread() {
			// TODO Auto-generated constructor stub
		}
		
		public void run() {			
		}
		
	}
}
